import { defineStore } from "pinia";
import { ref } from "vue";
import { userInfoApi } from "@/common/request/api/index";
export const useUserStore = defineStore(
  "user",
  () => {
    const token = ref("");
    const setToken = (newToken) => {
      token.value = newToken;
    };
    const userInfo = ref({});
    // 设置用户信息
    const setUserInfo = async (info) => {
      userInfo.value = info;
    };
    // 获取用户信息
    const getUserInfo = async (user) => {
      const res = await userInfoApi(user);
      userInfo.value = res;
    };
    // 清除用户信息
    const clearUserInfo = () => {
      userInfo.value = {};
    };
    return {
      token,
      setToken,
      userInfo,
      setUserInfo,
      getUserInfo,
      clearUserInfo,
    };
  },
  {
    // 开启持久化
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key);
        },
        setItem(key, value) {
          uni.setStorageSync(key, value);
        },
      },
    },
  }
);
